package com.smy.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.smy.reggie.common.R;
import com.smy.reggie.dto.SetmealDto;
import com.smy.reggie.pojo.Category;
import com.smy.reggie.pojo.Setmeal;
import com.smy.reggie.service.CategoryService;
import com.smy.reggie.service.SetmealDishService;
import com.smy.reggie.service.SetmealService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.stream.Collectors;

@Slf4j
@RestController
@RequestMapping("/setmeal")
public class SetmealController {
    @Autowired
    private SetmealService setmealService;
    @Autowired
    private SetmealDishService setmealDishService;
    @Autowired
    private CategoryService categoryService;

    /**
     * 和setmealDish表添加
     * @param dto
     * @return
     */
    @PostMapping
    //方法执行后清除缓存
    @CacheEvict(value = "setmeal_dish",allEntries = true)
    public R<String> addWhitDish(@RequestBody SetmealDto dto){
        setmealService.addWhitDish(dto);
        return R.success("添加成功!");
    }

    /**
     * 分页查询
     * @param page
     * @param pageSize
     * @param name
     * @return
     */
    @GetMapping("/page")
    @Transactional
    public R<Page<SetmealDto>> selectByPage(Long page,Long pageSize,String name){
        Page<Setmeal> setmealPage = new Page<>(page,pageSize);
        Page<SetmealDto> setmealDtoPage = new Page<>();
        LambdaQueryWrapper<Setmeal> wrapper = new LambdaQueryWrapper<>();
        wrapper.like(name != null,Setmeal::getName,name);
        wrapper.orderByDesc(Setmeal::getUpdateTime);
        setmealService.page(setmealPage,wrapper);
        BeanUtils.copyProperties(setmealPage,setmealDtoPage);
        List<Setmeal> records = setmealPage.getRecords();
        List<SetmealDto> recordDto = records.stream().map((item) ->{
            SetmealDto setmealDto = new SetmealDto();
            BeanUtils.copyProperties(item,setmealDto);
            Long categoryId = item.getCategoryId();
            Category category1 = categoryService.getById(categoryId);
            setmealDto.setCategoryName(category1.getName());
            return setmealDto;
        }).collect(Collectors.toList());
        setmealDtoPage.setRecords(recordDto);
        return R.success(setmealDtoPage);
    }

    @GetMapping("/list")
    @Cacheable(value = "setmeal_dish",key = "#setmeal.categoryId+'_'+#setmeal.status" )
    public R<List<Setmeal>> list(Setmeal setmeal){
        log.warn("获取前端请求");
        LambdaQueryWrapper<Setmeal> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(setmeal.getStatus() != null,Setmeal::getStatus,1);
        wrapper.eq(setmeal.getCategoryId() != null,Setmeal::getCategoryId,setmeal.getCategoryId());
        wrapper.orderByDesc(Setmeal::getUpdateTime);
        List<Setmeal> setmeals = setmealService.list(wrapper);
        return R.success(setmeals);
    }

    @DeleteMapping
    //方法执行后清除缓存
    @CacheEvict(value = "setmeal_dish",allEntries = true)
    public R<String> delete(@RequestParam List<Long> ids){
        setmealService.deleteWhitDish(ids);
        return R.success("删除成功!");

    }

}
